﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;

public class LoadExample3ByLabel : MonoBehaviour
{
    public IList<GameObject> waters;
    public AssetLabelReference waterLabel;
    // Start is called before the first frame update
    void Start()
    {
        Addressables.LoadAssetsAsync<GameObject>(waterLabel, OnResourcesCallback).Completed += OnResourcesRetrieved;
    }

    private void OnResourcesCallback(GameObject obj)
    {
        // 每个资源加载完成后会回调
        print("Callback:" + GetType() + obj);
    }

    private void OnResourcesRetrieved(AsyncOperationHandle<IList<GameObject>> obj)
    {
        // 这个标签的所有资源加载完会回调
        print("Completed:" + GetType() + obj.Result);
        waters = obj.Result;
        foreach (var gameObj in waters)
        {
            Vector3 position = Random.insideUnitSphere * 5;
            position.Set(position.x, 0, position.z);
            Instantiate(gameObj, position, Quaternion.identity, null);
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
